var Adv_skd=require("./Adv_skd3")

cc.Class({
  extends: cc.Component,
  properties: {
    buyTimesLabel: [cc.Label],
    priceLabel: cc.Label
  },
  initBy: function (e) {
        Adv_skd.default.instance.hideAllAdv();
    
     Adv_skd.default.instance.showInterstitialAd();
            Adv_skd.default.instance.showGridAdTop();
            Adv_skd.default.instance.showBannerAd();
    this.fromType = e;
    this.isLookAd = false;
    cc.pvz.PlayerData.checkAndResetPowetBuy();
    this.refreshUIInfo();
    cc.pvz.TAUtils.trackAdUIShow(cc.pvz.GameConfig.AdType.体力购买);
  },
  refreshUIInfo: function () {
    for (var e = 0; e < 2; e++) {
      this.buyTimesLabel[e].string = cc.pvz.GameConfig.PowerBuyTip.replace("$", cc.pvz.PlayerData.getPowerBuyTimes(0 == e));
    }
    var t = 3 - cc.pvz.PlayerData.getPowerBuyTimes(false);
    t = t < 0 ? 0 : t >= 3 ? 2 : t;
    this.diamondPrice = cc.pvz.GameConfig.PowerBuyPrices[t];
    this.priceLabel.string = "x" + this.diamondPrice;
  },
  onClickAdBuy: function () {
    var e = this;
    if (cc.pvz.PlayerData.getPowerBuyTimes(true) <= 0) {
      cc.popupManager.showToast("今日免费次数已用完");
    } else if (!this.isLookAd) {
      this.isLookAd = true, cc.pvz.AdUtils.showAdRewardVideo(cc.pvz.GameConfig.AdType.体力购买, function (t) {
        e.onAdClose(t);
      });
    }
  },
  onAdClose: function (e) {
    
    this.isLookAd = false;
    if (e) {
      cc.pvz.PlayerData.updatePowerBuyTimes(true, 1);
      this.refreshUIInfo();
      this.refreshAddPower(10);
    }
  },
  onClickDiamondBuy: function () {
    if (cc.pvz.PlayerData.getPowerBuyTimes(false) <= 0) {
      cc.popupManager.showToast("今日购买次数已用完");
    } else if (cc.pvz.PlayerData.isItemEnough(cc.pvz.GameConfig.ItemType.钻石, this.diamondPrice)) {
      cc.pvz.PlayerData.changeItemNum(cc.pvz.GameConfig.ItemType.钻石, -this.diamondPrice);
      cc.pvz.PlayerData.updatePowerBuyTimes(false, 1);
      this.refreshUIInfo();
      this.refreshAddPower(15);
    } else {
      cc.popupManager.showToast("钻石不足！");
    }
  },
  refreshAddPower: function (e) {
    cc.pvz.PlayerData.addMissionProgress(cc.pvz.GameConfig.MissionType.购买体力n次, 1);
    cc.pvz.PlayerData.addPower(e, true);
    if (this.fromType == cc.pvz.GameConfig.UIFromType.挑战副本) {
      cc.UIActLevelInfo.updateItemInfo();
      cc.UIActLevelInfo.checkPowerInfo();
    }
    cc.MainControl.updateItemInfo();
    cc.MainControl.checkPowerInfo();
    cc.popupManager.showEffectFly(cc.pvz.GameConfig.ItemType.体力, cc.MainControl.getItemEffectPos(cc.pvz.GameConfig.ItemType.体力));
    cc.butler.playEffect(cc.MainControl.rewardSound);
    cc.RedControl.onCheckBattleRed();
    cc.RedControl.onCheckMissionRed();
  },
  onCloseUI: function () {
            Adv_skd.default.instance.hideAllAdv();
    
    cc.popupManager.removePopup(this);
  }
});